<!DOCTYPE html>
  <head>
    <title>Flagship Jean JavaScript Unit Tests</title>
    <style type="text/css">
      .failed {
        color: #FF0000;
      }
    </style>
  </head>
  
  <body>
    <h1>JavaScript Unit Tests</h1>
    <ul id="results">
    </ul>
  </body>
  
  <script>
    function drawClock(date) {
      var h24, h12, m, ampm, date;
    
      // Get the time.
      h24 = date.getHours();
      m = date.getMinutes();
      
      // Convert to 12-hour time.
      h12 = h24 % 12;
      if (h12 == 0) {
        h12 = 12;
      }
      
      // Add a leading zero to single digit minutes. 
      if (m < 10) {
        m = '0' + m;
      }
      
      // Set the am/pm value.
      ampm = 'pm';
      if (h24 < 12) {
        ampm = 'am';
      }
      
      return h12 + ':' + m + ampm;
    }
    
    function parseWeek(data) {
      var i, len, sun, sat, tmp = [], week_val;
      
      for (i=0, len=data.length; i<len; i+=2) {
        // Parse to remove leading zeroes.
        sun = data[i].replace(/-0/g, '-');
        sat = data[i+1].replace(/-0/g, '-');
      
        tmp.push(sun);
        tmp.push(sat);
      
        // Split up the dates.
        sun = sun.split('-');
        sat = sat.split('-');
         
        // Update weeks.
        week_val = 'wlink-' + sun[1] + '-' + sun[2]
        tmp.push(week_val);
        
        // Add to the monthly display.
        tmp.push(sun[1] + '/' + sun[2] + ' - ' + sat[1] + '/' + sat[2]);
      }
      
      return tmp;
    }
    
    // Outputs a message.
    function printMsg(msg, failed) {
      var li = document.createElement('li');
      
      if (failed) {
        li.className = 'failed';
      }
      
      li.innerHTML = msg;
      
      document.getElementById('results').appendChild(li);
    }
    
    // Lets run some tests.
    
    // ====================
    // Test the time display.
    var dates = [new Date(2010, 12, 1, 0, 0, 0, 0), new Date(2010, 12, 1, 1, 5, 0, 0), new Date(2010, 12, 1, 8, 40, 0, 0), new Date(2010, 12, 1, 12, 1, 0, 0), new Date(2010, 12, 1, 13, 8, 0, 0), new Date(2010, 12, 1, 22, 5, 0, 0), new Date(2010, 12, 1, 23, 30, 0, 0)];
    var expected_dates = ['12:00am', '1:05am', '8:40am', '12:01pm', '1:08pm', '10:05pm', '11:30pm'];
    var correct_dates = 0;
    
    for (i=0; i<dates.length; ++i) {
      if (expected_dates[i] == drawClock(dates[i])) {
        ++correct_dates;
      } else {
        printMsg("Date test failed.. expected " + expected_dates[i] + " got " + drawClock(dates[i]), true);
      }
    }
    
    if (correct_dates  == dates.length) {
      printMsg(correct_dates + "/" + correct_dates + " date tests passed - <strong>PASS</strong>");
    } else {
      printMsg(correct_dates + "/" + dates.length + " date tests passed - <strong>FAIL</strong>", true);
    }
    
    // ====================
    // Test the week parser.
    var weeks = [["2010-05-02","2010-05-08","2010-05-09","2010-05-15"], ["2010-11-02","2010-11-08","2010-11-09","2010-11-15"]];
    var expected_weeks = [["2010-5-2", "2010-5-8", "wlink-5-2", "5/2 - 5/8", "2010-5-9", "2010-5-15", "wlink-5-9", "5/9 - 5/15"], ["2010-11-2", "2010-11-8", "wlink-11-2", "11/2 - 11/8", "2010-11-9", "2010-11-15", "wlink-11-9", "11/9 - 11/15"]];
    var correct_weeks = 0;
    
    for (i=0; i<weeks.length; ++i) {
      var p = parseWeek(weeks[i]),
      good = true;
      
      for (j=0; j<p.length; ++j) {
        if (p[j] != expected_weeks[i][j]) {
          good = false;
          printMsg("Week parse test failed.. expected " + expected_weeks[i][j] + " got " + p[j], true);
        }
      }
      
      correct_weeks = good ? correct_weeks + 1 : correct_weeks;
    }
    
    if (correct_weeks  == weeks.length) {
      printMsg(correct_weeks + "/" + weeks.length + " week parse tests passed - <strong>PASS</strong>");
    } else {
      printMsg(correct_weeks + "/" + weeks.length + " week parse tests passed - <strong>FAIL</strong>", true);
    }
  </script>
</html>
